/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BusinessLogic;

import java.util.Vector;
import DTO.Exhibitor;
import DTO.Product;
import DataAccess.DataUtility;
import java.sql.ResultSet;
import java.util.HashMap;
/**
 *
 * @author Tran anh
 * @version 1.0
 */
public class ProductManagement {

    /**
     * Create a vector used to contain list products
     */
    private Vector<Product> listProduct;

    /**
     * create a vector used to contain list Exhibitors
     */
    private Vector<Exhibitor> listExhibitor;

    public ProductManagement(){
        
    }

    /**
     * Load list product from database and idExhibtor same with
     * idExhibitor be got from nameExhibitor
     * @param nameExhibitor is specify a Exhibitor
     * @return list product
     */
    public Vector<Product> loadProduct(String nameExhibitor) {
        try {
            listProduct = new Vector<Product>();
            //get idExhibitor from name
            String idExhibitor = getID(nameExhibitor);
            
            ResultSet rs = DataUtility.excuteQuery("Select * from Product where idExhibitor = " + "\'" + idExhibitor + "\'");
            while (rs.next()) {
                Product product = new Product();
                listProduct.add(product);

                product.setId(rs.getString("id"));
                product.setDateCreate(rs.getString("DateCreate"));
                product.setDecription(rs.getString("Decription"));
                product.setIdExhibitor(rs.getString("IdExhibitor"));
                product.setName(rs.getString("Name"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return listProduct;
    }

    /**
     * Get idExhibitor from name in listExhibitor
     * @param nameExhibitor is name of a Exhibitor
     * @return id of that Exhibitor
     */
    public String getID(String nameExhibitor){
        for(Exhibitor ex : listExhibitor){
            if(ex.getName().equalsIgnoreCase(nameExhibitor)){
                return ex.getId();
            }
        }
        return "";
    }

    /**
     * Load list Exhibitor from database, every exhibitor must be has
     * status = 1
     * @return list Exhibitor
     */
    public Vector<Exhibitor> loadExhibitor() {
        try {
            listExhibitor = new Vector<Exhibitor>();

            ResultSet rs = DataUtility.excuteQuery("Select * from Exhibitor where status = 1");
            while (rs.next()) {
                Exhibitor exhibitor = new Exhibitor();
                listExhibitor.add(exhibitor);

                exhibitor.setId(rs.getString("id"));
                exhibitor.setName(rs.getString("Name"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return listExhibitor;
    }

    /**
     * Filter list exhibitor folow by a string, if name of a Exhibitor
     * containt that string name will be add into subList
     * @param name is String
     * @return list exhibitor containt this string
     */
    public Vector<Exhibitor> filterExhibitor(String name){
        Vector<Exhibitor> subList = new Vector<Exhibitor>();
        name = name.toUpperCase().trim();

        for(Exhibitor exhibitor : listExhibitor){
            String nameEx = exhibitor.getName();
            nameEx = nameEx.toLowerCase().trim();
            if(nameEx.contains(name)){
                subList.add(exhibitor);
            }
        }
        return subList;
    }

    /**
     * Generate new ID of Product and not same other Id has in database
     * @return String new ID
     */
    public String createID(){
        String id = "PR";
        int amount = 0;
        try {
            ResultSet rs = DataUtility.excuteQuery("select count(*) from Product");
            rs.next();
            amount = rs.getInt(1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String number = ""+ (amount + 1);
        if(number.length() == 1)
            id = id + "000";
        else if(number.length() == 2)
            id = id + "00";
        else if(number.length() == 3)
            id = id + "0";
        id = id + number;
        return id;
    }

    /**
     * Return a product folow by it's index in array
     * @param index is specify location of it in array
     * @return a object product
     */
    public Product getByIndex(int index) {
        return listProduct.get(index);
    }

    /**
     * Get information of object Product put into HashMap.
     * Then call method executeProcedure to insert object into database
     * @param product is new Object to get information pass into database
     */
    public void insert(Product product){
        HashMap fiels = new HashMap();

        fiels.put("dateCreate", product.getDateCreate());
        fiels.put("Decription", product.getDecription());
        fiels.put("id", product.getId());
        fiels.put("IdExhibitor", product.getIdExhibitor());
        fiels.put("Name", product.getName());

        DataUtility.executeProcedure("insertProduct", fiels);
    }

    /**
     * Get information of object Product put into HashMap.
     * Then call method executeProcedure to update object into database
     * @param product is new Object to get information pass into database
     */
    public void update(Product product){

    }

    
}
